// src/api/resignApply.js
import axios from 'axios'

// 创建 axios 实例
const service = axios.create({
  baseURL: "http://localhost:8080/", // Vue3 中使用 import.meta.env
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    // 示例：添加 token 认证
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    console.error('请求错误:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    const res = response.data
    // 假设后端约定 code=200 为成功
    if (res.code !== 200) {
      console.error('接口错误:', res.message || '请求失败')
      return Promise.reject(new Error(res.message || 'Error'))
    }
    return res
  },
  (error) => {
    console.error('响应错误:', error)
    return Promise.reject(error)
  }
)

// 获取离职申请列表
export const getResignApplyList = () => {
  return service({
    url: '/resignApply/list',
    method: 'get'
  })
}

// 根据 ID 获取离职申请详情（扩展功能）
export const getResignApplyDetail = (resignId) => {
  return service({
    url: `/resignApply/detail/${resignId}`,
    method: 'get'
  })
}
// api/resignApply.js
export const agreeResignApply = async (resignId) => {
  return await service.put(`/resignApply/agree/${resignId}`)
}

export const rejectResignApply = async (resignId) => {
  return await service.put(`/resignApply/reject/${resignId}`)
}

export default service